package com.way.filemanager.online;

import hwdroid.widget.item.Item.Type;
import hwdroid.widget.item.WidgetText2Item;
import hwdroid.widget.itemview.WidgetText2ItemView;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.PixelFormat;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.way.filemanager.R;
import com.way.filemanager.util.AndroidHelper;
import com.way.filemanager.util.CommonFunc;
import com.way.filemanager.util.CommonIdDef;


public class FileListAdapter extends BaseAdapter {

    private static final String TAG = "FileListAdapter";
    //private FileListAdapterItemClick mListener;
    //private List<OnlineFileInfo> fileInfoList;
    private Context mContext;
    private LinearLayout KanboxView;
    int mFileType;
    int mAdapterType;
    OnlineFilelistManager mFileListManager;
    LinearLayout mRightViewGroup;
    RelativeLayout mLeftViewGroup_photo;
    RelativeLayout mLeftViewGroup_music;
    RelativeLayout mLeftViewGroup_video;
    RelativeLayout mLeftViewGroup_apk;
    RelativeLayout mLeftViewGroup_zip;
    RelativeLayout mLeftViewGroup_unknown;
    RelativeLayout mLeftViewGroup_txt;
    RelativeLayout mLeftViewGroup_txt_doc;
    RelativeLayout mLeftViewGroup_txt_ppt;
    RelativeLayout mLeftViewGroup_txt_pdf;
    RelativeLayout mLeftViewGroup_txt_xls;
    LinearLayout getLeftViewGroup(int type)
    {
        LayoutInflater mInflater = LayoutInflater.from(mContext);
        LinearLayout itemview = (LinearLayout)mInflater.inflate(R.layout.item_left_viewgroup, null);
        ImageView icon = (ImageView)itemview.findViewById(R.id.left_drawable);
        icon.setImageResource(type);
        return itemview;

    }
    LinearLayout getLeftViewGroup(Drawable dwIcon)
    {
        LayoutInflater mInflater = LayoutInflater.from(mContext);
        LinearLayout itemview = (LinearLayout)mInflater.inflate(R.layout.item_left_viewgroup, null);
        ImageView icon = (ImageView)itemview.findViewById(R.id.left_drawable);
        icon.setImageDrawable(dwIcon);;
        return itemview;

    }
    public FileListAdapter(Context ctx,OnlineFilelistManager m) {
        super();
        mContext = ctx;
        mFileListManager = m;
        //mListener = listener;
    }


   /* public void setData(List<OnlineFileInfo> list) {
        fileInfoList = list;
    }*/


    @Override
    public int getCount() {

        /*if (fileInfoList == null) {
            return 0;
        } else {
            return fileInfoList.size();
        }*/
        int AddItemNum = 0;
        switch(mAdapterType)
        {
            case CommonIdDef.CATEGORY_SUBTYPE_MODE_NORMAL:
                AddItemNum = 1;
                break;
            case CommonIdDef.CATEGORY_SUBTYPE_MODE_SEARCH_BTN:
                AddItemNum = 1;
                break;
            case CommonIdDef.CATEGORY_SUBTYPE_MODE_SEARCHING:
                return mFileListManager.getSearchList().size();
            default:
                AddItemNum = 1;
                break;
        }
        if(mFileListManager != null)
            return mFileListManager.GetWorkingFileCount() + AddItemNum;
        else
            return 0;
    }

    /*@Override
    public Object getItem(int i) {
        if (fileInfoList == null) {
            return null;
        } else {
            return fileInfoList.get(i);
        }
    }

    @Override
    public long getItemId(int i) {
        if (fileInfoList == null) {
            return 0;
        } else {
            return i;
        }
    }*/
    public void SetAdapterMode(int type)
    {
        mAdapterType = type;
    }
    public int GetAdapterMode()
    {
        return mAdapterType;
    }
      @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            //BugID:103083: add wording (kanbox support) below filelist
            if(KanboxView == null){
                KanboxView = new LinearLayout(mContext);
                KanboxView.setOrientation(LinearLayout.VERTICAL);
                TextView tv = new TextView(mContext);
                tv.setText(R.string.support_by_kupan);
                tv.setTextSize(14);
                tv.setGravity(Gravity.CENTER_HORIZONTAL);
                LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT);
                lp.topMargin = (int )AndroidHelper.convertDpToPixel(mContext, 10);
                lp.bottomMargin = (int )AndroidHelper.convertDpToPixel(mContext, 10);
                lp.gravity = Gravity.CENTER_HORIZONTAL;
                KanboxView.addView(tv, lp);
            }
            if(position == mFileListManager.GetWorkingFileCount() ){
                return KanboxView;
            }
            if(KanboxView == convertView ){
                convertView = null;
            }
            OnlineFileInfo infoitem;
            if(mAdapterType == CommonIdDef.CATEGORY_SUBTYPE_MODE_SEARCHING)
            {
                infoitem = mFileListManager.getSearchList().get(position);
            }else if(mAdapterType == CommonIdDef.CATEGORY_SUBTYPE_MODE_SELECTING)
            {
                infoitem = mFileListManager.getSelectList().get(position);
            }
            else {
                infoitem = mFileListManager.GetCurrentFileInfo(position);
            }
            if(infoitem != null)
            {
                String filename = infoitem.mfileName;
                WidgetText2Item item = new WidgetText2Item();
                item.mText = filename;
                if(infoitem.mIsFolder){
                    item.mSubText = CommonFunc.CreateTimeFormat(infoitem.mCreationDate*1000);
                }else{
                    item.mSubText = CommonFunc.FormetFileSize(infoitem.mFileSize)+ "|" + CommonFunc.CreateTimeFormat(infoitem.mCreationDate*1000);
                }

                item.leftviewgroup = null;//mContext.getResources().getDrawable(R.drawable.fso_type_document);
                //Log.d(TAG,"getView filename  is "+infoitem.mfileName+",from manager type is "+mFileListManager.getFileType(filename)
                       // +",haveDownload is "+infoitem.mDownloaded);
                switch (mFileListManager.getFileType(filename)) {
                case CommonIdDef.CATEGORY_ITEM_ID_PICTURE:
                    if(infoitem.mHasThumbnail && !TextUtils.isEmpty(infoitem.mThumbnailPath)){
                        Drawable thumbDrawable = Drawable.createFromPath(infoitem.mThumbnailPath);
                        //thumbDrawable = zoomDrawable(thumbDrawable,360 , 360);//use larege mode in thumb,no need zoom
                        if(thumbDrawable!=null){
                            //item.leftDrawable = thumbDrawable;
                            item.leftviewgroup = getLeftViewGroup(thumbDrawable);
                            //Log.d(TAG,"getView use download thumb break");
                            break;
                        }
                    }
                    //Log.d(TAG,"getView not use download thumb");
                    item.leftviewgroup = getLeftViewGroup(mContext.getResources().getDrawable(R.drawable.ic_photo_normal));
                    break;
                case CommonIdDef.CATEGORY_ITEM_ID_MUSIC:
                    item.leftviewgroup = getLeftViewGroup(mContext.getResources().getDrawable(R.drawable.ic_music_normal));
                    break;
                case CommonIdDef.CATEGORY_ITEM_ID_VIDEO:
                    item.leftviewgroup = getLeftViewGroup(mContext.getResources().getDrawable(R.drawable.ic_video_normal));
                    break;
                case CommonIdDef.CATEGORY_ITEM_ID_PACKAGE:
                    item.leftviewgroup = getLeftViewGroup(mContext.getResources().getDrawable(R.drawable.ic_apk_normal));
                    break;
                case CommonIdDef.CATEGORY_ITEM_ID_TEXT:
                    if(filename.endsWith("pdf")){
                        item.leftviewgroup = getLeftViewGroup(mContext.getResources().getDrawable(R.drawable.ic_pdf_normal));
                    }else if(filename.endsWith("doc") || filename.endsWith("docx")){
                        item.leftviewgroup = getLeftViewGroup(mContext.getResources().getDrawable(R.drawable.ic_doc_normal));
                    }else if(filename.endsWith("xls") || filename.endsWith("xlsx")){
                        item.leftviewgroup = getLeftViewGroup(mContext.getResources().getDrawable(R.drawable.ic_xls_normal));
                    }else if(filename.endsWith("ppt") || filename.endsWith("pptx")){
                        item.leftviewgroup = getLeftViewGroup(mContext.getResources().getDrawable(R.drawable.ic_ppt_normal));
                    }else{
                        item.leftviewgroup = getLeftViewGroup(mContext.getResources().getDrawable(R.drawable.ic_txt_normal));
                    }

                    break;
                case CommonIdDef.CATEGORY_ITEM_ID_ZIP:
                    item.leftviewgroup = getLeftViewGroup(mContext.getResources().getDrawable(R.drawable.ic_zip_normal));
                    break;
                default:
                    if(infoitem.mIsFolder){
                        item.leftviewgroup = null;//mContext.getResources().getDrawable(R.drawable.aui_ic_folder_normal);
                    }else {
                        item.leftviewgroup = getLeftViewGroup(mContext.getResources().getDrawable(R.drawable.ic_unknown_normal));
                    }

                    break;
                }

                if(mAdapterType == CommonIdDef.CATEGORY_SUBTYPE_MODE_SELECTING)
                {
                    LayoutInflater mInflater = LayoutInflater.from(mContext);
                    item.rightviewgroup = (LinearLayout) mInflater.inflate(R.layout.item_right_viewgroup_checkbox, null);
                    //item.setTypeMode(Type.CHECK_MODE);
                    CheckBox mCheckBox = (CheckBox)item.rightviewgroup.findViewById(R.id.rightItem_Checkbox);
                    mCheckBox.setChecked(infoitem.mSelected);
                    mCheckBox.setClickable(false);
                }
                else
                {
                    if(infoitem.mIsFolder)
                    {
                        LayoutInflater mInflater = LayoutInflater.from(mContext);
                        LinearLayout mRightViewGroup_temp = (LinearLayout) mInflater.inflate(R.layout.item_right_viewgroup, null);
                        item.rightviewgroup = mRightViewGroup_temp;//mContext.getResources().getDrawable(R.drawable.ic_list_enter_normal);
                    }
                    else
                        item.rightviewgroup = null;
                    //item.setTypeMode(Type.NORMAL_MODE);
                }

                if(infoitem.mDownloaded){
                    //item.leftDrawable = mContext.getResources().getDrawable(R.drawable.ic_unknown_normal);
                    ImageView flag = (ImageView)item.leftviewgroup.findViewById(R.id.download_flag);
                    flag.setVisibility(View.VISIBLE);
                    //Log.d(TAG,"download getView flag is true");
                    //((WidgetText2ItemView)convertView).setDownloadFlag(true);
                }
                
                if (convertView == null) {
                    WidgetText2ItemView itemview = (WidgetText2ItemView)item.newView(mContext, null);
                    itemview.prepareItemView();
                    convertView = itemview;
                }
                item.mWidgetTagKey = R.id.widget_text2_item_view_tag_key;
                ((WidgetText2ItemView)convertView).setObject(item);

            }
            convertView.setId(position);
            return convertView;
        }


    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }


    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }
    private Bitmap drawableToBitmap(Drawable drawable)
    {
              int width = drawable.getIntrinsicWidth();
              int height = drawable.getIntrinsicHeight();
              //Log.d(TAG,"get thumb init width is "+width+",height is "+height);
              Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888:Bitmap.Config.RGB_565;
              Bitmap bitmap = Bitmap.createBitmap(width, height, config);
              Canvas canvas = new Canvas(bitmap);
              drawable.setBounds(0, 0, width, height);
              drawable.draw(canvas);
              return bitmap;
    }

    private Drawable zoomDrawable(Drawable drawable, int w, int h)
    {
              int width = drawable.getIntrinsicWidth();
              int height= drawable.getIntrinsicHeight();
              Bitmap oldbmp = drawableToBitmap(drawable);
              Matrix matrix = new Matrix();
              float scaleWidth = ((float)w / width);
              float scaleHeight = ((float)h / height);
              //Log.d(TAG,"get thumb zoomDrawable dst width is "+w+",height is "+h+",scaleWidth is "+scaleWidth+",scaleHeight is "+scaleHeight);
              matrix.postScale(scaleWidth, scaleHeight);
              Bitmap newbmp = Bitmap.createBitmap(oldbmp, 0, 0, width, height, matrix, true);
              return new BitmapDrawable(newbmp);
    }

}
